import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

/**
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'el-icon-s-data' }
      }
    ]
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }

]
export const asyncRoutes = [
  {
    path: '/school',
    redirect: 'index',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'school',
        component: () => import('@/views/school/index'),
        meta: { title: '学校管理', icon: 'el-icon-office-building', roles: ['manager'] }
      }
    ]
  },
  {
    path: '/people',
    name: 'people',
    meta: { title: '人员管理', icon: 'el-icon-user', roles: ['manager', 'teacher'] },
    component: Layout,
    children: [
      {
        path: 'manager',
        name: 'manager',
        component: () => import('@/views/people/manager'),
        meta: { title: '超级用户管理', roles: ['manager'] }        
      },
      {
        path: 'teacher',
        name: 'teacher',
        component: () => import('@/views/people/teacher'),
        meta: { title: '教师管理', roles: ['manager'] }
      },
      {
        path: 'student',
        name: 'student',
        component: () => import('@/views/people/student'),
        meta: { title: '学生管理', roles: ['teacher'] }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
